package hn.cch.spring.aop.proxy.proxy;


import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.Arrays;

public class DynamicProxyUtil implements MethodInterceptor {


    public Object getProxy(Class<?> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(this);
        enhancer.setSuperclass(clazz);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("before:" + Arrays.toString(objects));
        Object invoke = methodProxy.invokeSuper(o, objects); // 通过代理类调用父类的方法
        System.out.println("after:" + invoke);
        return invoke;
    }
}
